<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2019/1/18  14:42
 */
namespace app\api\logic;
use app\api\BaseModel;
use app\api\model\DriverOrderInfo;

class Lineorder extends  BaseModel
{
    /*
    * 线路订单
    */
    public function Orderlist()
    {
        $page=isset($this->requestData['page'])?$this->requestData['page']:'1';
        $limit=isset($this->requestData['limit'])?$this->requestData['limit']:'10';
//        $status = isset($this->requestData['status'])?$this->requestData['status']:'0';
        $time = isset($this->requestData['time'])?$this->requestData['time']:'1';
        $today = strtotime(date("Y-m-d"),time());//1547049600
        $zuotian = strtotime(date("Y-m-d"),time())-24*60*60;//1546963200
//        var_dump($today);
//        var_dump($zuotian);
//        die;
//        switch($time){
//            case '1':
//                $where['send_time'] = ['>=',$today];
//                break;
//            case '2':
//                $where['send_time'] = ['<',$today];
//                $where['send_time'] = ['>=',$zuotian];
//                break;
//            case '3':
//                $where['send_time'] = ['<',$zuotian];
//                break;
//            default:
//                $where['send_time'] = ['>=',$today];
//        }

//        switch ($status) {
//            case '0':
//                //待接订单
//                $where['status'] = '0';
//                break;
//            case '1':
//                //已接订单
//                $where['status'] = '1';
//                break;
//            case '2':
//                //已完成订单
//                $where['status'] = '2';
//                break;
//            case '-1':
//                //已完成订单
//                $where['status'] = '-1';
//                break;
//            default:
//                //进行中订单
//                $where['status'] = '1';
//                break;
//        }
        $where['status'] = ['in',[0,1,-1]];
        $where['shop_id']=$this->user['shopid'];
        //订单数量
        $result = $this->getDrivarOrder($where,$page,$limit);
        return $result;

    }

    public function orderInfo()
    {
        $id = isset($this->requestData['id'])?$this->requestData['id']:'0';
        $uid = $this->user['shopid'];
        if($id){
            $sel = db('driver_order')->where(['id'=>$id,'shop_id'=>$uid])->find();
            if(!$sel) return ['message'=>'数据不存在'];
        }else{
            return ['message'=>'id必传!'];
        }
        $order = db('driver_order_info')->where(['driver_order_id'=>$id])->order('distance asc')->select();
//var_dump($order);
        foreach($order as $k=>&$v){
            $order_id['id'] = $v['order_id'];
            $v['order'] =  db('order')->where($order_id)->field('id,order_sn,address,lng,lat,mobile,realname,send_status')->find();
//            var_dump($order[$k]['order']);die;
        }

        $result['driver_id'] = $sel['driver_id'];
        $result['driver_nickname'] = db('driver')->where(['id'=>$sel['driver_id']])->value('nickname');
        $result['list'] = $order;
        return $result;
    }

    /*
     * 重选司机
     */
    public function resetDriver()
    {
        $id = isset($this->requestData['id'])?$this->requestData['id']:'0';
        $driver_id = isset($this->requestData['driver_id'])?$this->requestData['driver_id']:'0';
        $driver_shop_where = ['shop_id'=>$this->user['shopid'],'driver_id'=>$driver_id,'status'=>1,'isbind'=>1];
        $res_driver_shop = db('driver_shop')->where($driver_shop_where)->find();
        $where['id'] = $id;
        $where['shop_id'] = $this->user['shopid'];
        $db = db('driver_order');
        $res = $db->where($where)->find();
        if(empty($res))return ['message'=>'非法ID!'];
        if($res['status'] == 2)return ['message'=>'该单已完成'];
        $data = [
            'driver_id'=>$driver_id,
            'status'=>0
        ];
        $res = $db->where($where)->update($data);
        if(!$res)return ['message'=>'重选司机失败'];
    }

    public function getBeatRoute()
    {
        $search = [];//查出的存放点
        $shopid=$this->user['shopid'];
        $shop = db('shop')->where(['id'=>$shopid])->field('lat,lng')->find();
        //['lat'=>123.22,'lng'=>143.55];
        $top = $shop;//仓库点

        //筛选的数据
        $id = isset($this->requestData['id'])?$this->requestData['id']:'0';
        $where['shop_id']=$shopid;
        $where['id'] = $id;
//        $uid = 3;
        $driverorder = db('driver_order')->where($where)->find();
//        echo db('driver_order')->getLastSql();
        if(empty($driverorder))return ['message'=>'参数有误'];

        $ar = db('driver_order_info')->alias('do')
            ->join('order o', 'o.id = do.order_id')
            ->where(['do.driver_order_id'=>$id])
            ->field('do.*,o.lat,o.lng')
            ->select();

        $lenght = count($ar);
        if($lenght == 1){
            $ar[0]['dif'] = getDistance($top['lat'],$top['lng'],$ar[0]['lat'],$ar[0]['lng']);
            $search=$ar;
        }else{
            //for($i=0; $i<$lenght;$i++){
                //找出第一点
                foreach($ar as $k=>&$v){
                    //计算距离
                    $v['dif'] = getDistance($top['lat'],$top['lng'],$v['lat'],$v['lng']);
                    // $v['dif'] = $v['a'] * $v['b'] - $top['a']*$top['b'];
                    $dif[$k] = $v['dif'];
                }
                array_multisort($dif, SORT_ASC, SORT_NUMERIC, $ar);
                $search = $ar;
                //$top = $ar[0];
                unset($dif);
                //unset($ar[0]);
            //}

        }

        if(!empty($search)){
            foreach ($search as $key => $value) {
                $data[$key]['id'] = $value['id'];
                $data[$key]['num'] = $key+1;
                $distance = round($value['dif']/1000,2);
                $data[$key]['distance'] = $distance;
            }

            $driver_order_info = new DriverOrderInfo();
            $res = $driver_order_info->saveAll($data);
            if(!$res)return ['message'=>'操作失败'];
        }
    }

    public function getDrivarOrder($where, $page, $limit)
    {

        //订单数量
        $count = db('driver_order')->where($where)->count();

        $order = db('driver_order')
            ->where($where)
            ->order('id desc')
            ->page($page,$limit)
            ->select();
//         ECHO $this->getLastSql();EXIT;
        foreach($order as $key=>&$value){
            $value['driver_info'] = db('driver')->where(['id'=>$value['driver_id']])->field('id,nickname,address,mobile,headimage,sex')->find();
            $value['send_time'] = date('Y-m-d h:i:s',$value['send_time']);
            if($value['finish_time']){date('Y-m-d h:i:s',$value['finish_time']);}
            //订单数量
            $value['order_count'] = db('driver_order_info')->where(['driver_order_id'=>$value['id']])->count();
        }
        return array('list' => array_merge($order), 'page_num' => $page, 'page_limit' => $limit, 'count' => $count);
    }
}